import Ember from 'ember';
import Changeset from 'ember-changeset';
import WarningValidations from '../../../validations/warning';
import lookupValidator from 'ember-changeset-validations';

export default Ember.Controller.extend(WarningValidations, {
    constants: Constants,
    detailEdit: false,
    delFlag: true,
    dateService: Ember.inject.service("date-service"),
    store: Ember.inject.service("store"),
    // warningModel: Ember.computed("detailEdit","warning.id", function() {
    //     var model = this.get("warning");
    //     console.log("model warning", model);
    //     if (!model) {
    //         return null;
    //     }
    //     return new Changeset(model, lookupValidator(WarningValidations), WarningValidations);
    // }),
    warningObs: function() {
        var model = this.get("warning");
        console.log("model warning", model);
        if (!model) {
            return null;
        }
        var warningModel = new Changeset(model, lookupValidator(WarningValidations), WarningValidations);
        this.set("warningModel", warningModel);
    }.observes("warning"),
    refreshStaffList: function() {
        var route = App.lookup('route:business.mainpage.warning-management');
        //route.refresh();
        App.lookup('controller:business.mainpage').refreshPage(route);
    },

    // init() {
    //     this._super(...arguments);
    //     let model = this.get("store").createRecord('hbeaconwarning', {});
    //     // this.set("customerInComp",model);
    //     this.warning = model;
    // },
    abd: function() {},
    defaultCustomer: Ember.computed('warning.caller', 'customerListFirst', function() {
        //if (this.get('warning.caller') && this.get('warning.caller').content) {
        return this.get('warning.caller');
        //}
        //return this.get('customerListFirst');
    }),
    defaultStaff: Ember.computed('warning.operater', 'staffListFirst', function() {
        //if (this.get('warning.operater') && this.get('warning.operater').content) {
        return this.get('warning.operater');
        //}
        //return this.get('staffListFirst');
    }),
    //预警时间验证
    warningTimeValidate() {
        var callTime = this.get('warning.callTime');
        if (!callTime || callTime.length === 0) {
            this.set("warning.page_errors.title", "预警时间不能为空");
            this.get("warning").incrementProperty("page_errorsCnt");
            return false;
        } else {
            this.set("warning.page_errors.title", null);
        }
        return true;
    },
    actions: {
        invalid() {
            //alert("invalid");
        },
        //编辑按钮
        detailEditClick: function(warning) {
            this.set('detailEdit', true);
        },
        //取消按钮
        detailCancel: function() {
            var id = this.get('id');
            var editMode = this.get('editMode');
            var mainpageController = App.lookup('controller:business.mainpage');
            this.set('detailEdit', false);
            //alert("详情");
            if (id && editMode == 'edit') {
                this.get("warning").rollbackAttributes();
                this.set("warningModel", new Changeset(this.get("warning"), lookupValidator(WarningValidations), WarningValidations));
            } else {
                mainpageController.switchMainPage('warning-management', {});
            }
        },
        //存储
        saveWarning() {
            //alert("保存");
            console.log("++++++++++++delFlag+++++++++++++", this.get("delFlag"));
            if (this.get("delFlag")) {
                //alert("保存进来了");
                var _self = this;
                var warningModel = this.get("warningModel");
                var mainpageController = App.lookup('controller:business.mainpage');
                var editMode = this.get('editMode');
                var id = this.get('id');
                warningModel.validate().then(function() {
                    //alert("save   out");
                    if (warningModel.get('errors.length') === 0) {
                        App.lookup('controller:business.mainpage').openPopTip("正在保存");
                        warningModel.save().then(function() {
                            App.lookup('controller:business.mainpage').showPopTip("保存成功");
                            if (id && editMode == 'edit') {
                                console.log("id is", id);
                                console.log("editMode is", editMode);
                                _self.set('detailEdit', false);
                            } else {
                                mainpageController.switchMainPage('warning-management');
                                _self.set('detailEdit', false);
                            }
                        });

                    } else {
                        warningModel.set("validFlag", Math.random());
                    }
                });
            }
        },
        //是否处理
        flagSelect(flagDict) {
            this.get("warning").set("flag", flagDict);
        },
        //用户姓名
        selectParent(caller) {
            this.get('warningModel').set("caller", caller);
            this.set("defaultCustomer", caller);
            //console.log("+++++name++++++",name);
        },
        //选择床位号
        selectBedParent(customer) {
            this.set("customer", customer);
        },
        //处理人
        selectStaff(staff) {
            this.set("staff", staff);
            this.get("warning").set("operater", staff);
        },
        changeCallTimeAction(date) {
            console.log("date is who", date);
            var stamp = this.get("dateService").timeToTimestamp(date);
            this.set("warningModel.callTime", stamp);
        },
        //删除按钮
        delById: function(warning) {
            //this.set('showpopInvitePassModal', true);
            this.set("delFlag", false);
            var _self = this;
            App.lookup('controller:business.mainpage').showConfirm("是否确定删除此床位记录", function() {
                App.lookup('controller:business.mainpage').openPopTip("正在删除");
                warning.set("delStatus", 1);
                warning.save().then(function() {
                    App.lookup('controller:business.mainpage').showPopTip("删除成功");
                    _self.set("delFlag", true);
                    //console.log("delFlag删除", _self.get("delFlag"));
                    var mainpageController = App.lookup('controller:business.mainpage');
                    mainpageController.switchMainPage('warning-management');
                });
            });
        },
        dpShowAction(e) {

        },
        //弹窗取消
        // invitation() {
        //     this.set('showpopInvitePassModal', false);
        //     this.set("delFlag", true);
        // },
        //弹窗确定，删除记录
        // cancelPassSubmit(warning) {
        //     var _self = this;
        //     App.lookup('controller:business.mainpage').openPopTip("正在删除");
        //     this.set("showpopInvitePassModal", false);
        //     warning.set("delStatus", 1);
        //     warning.save().then(function() {
        //         App.lookup('controller:business.mainpage').showPopTip("删除成功");
        //         _self.set("delFlag", true);
        //         console.log("delFlag删除", _self.get("delFlag"));
        //         var mainpageController = App.lookup('controller:business.mainpage');
        //         mainpageController.switchMainPage('warning-management');
        //
        //     });
        // },

    }
});
